package cn.krui.helper.tools;

import java.util.Set;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import cn.krui.helper.bean.PublisterBean;
import cn.krui.helper.pub_sub.Publisher;

/**
 * @author zc
 */
@Component
public class ApplicationRunnerX implements ApplicationRunner {

    private static final Logger logger = LoggerFactory.getLogger(ApplicationRunnerX.class);
    private StringRedisTemplate redisTemplate;
    private Publisher publisher;
    private String prefix;

    public ApplicationRunnerX(StringRedisTemplate redisTemplate,Publisher publisher,String _prefix){
        this.redisTemplate = redisTemplate;
        this.publisher = publisher;
        this.prefix = _prefix;
    }
    
    @Override
    public void run(ApplicationArguments args) throws Exception {
        logger.info("项目启动检查消息队列发送check消息");
        RedisUtils redisUtils = new RedisUtils(redisTemplate, prefix);
        Set<String> pub_set = redisUtils.setMembers(String.format("pub:pub_set"));
        if (pub_set == null) {
            return;
        }
        for (String string : pub_set) {
            Set<String> pub_event_set = redisUtils.setMembers(String.format("pub:%s_set", string));
            if (pub_event_set == null) {
                continue;
            }
            for (String string2 : pub_event_set) {
                Set<String> pub_event_url_set = redisUtils.setMembers(String.format("pub:%s:%s_set", string, string2));
                if (pub_event_url_set == null) {
                    continue;
                }
                for (String string3 : pub_event_url_set) {
                    if (StringUtils.isEmpty(string3)) {
                        continue;
                    }
                    PublisterBean<String> data = new PublisterBean<String>(string2,"check");
                    data.setExecUrl(string3);
                    publisher.pub(string, data);
                }
            }
        }
        logger.info("完成消息队列检查");
    }
}